package some.coupon.member.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import some.coupon.member.dao.MCouponDao;
import some.coupon.member.domain.MCouponCommand;

@Controller
public class UseCouponController {
	
	@Autowired
	private MCouponDao mCouponDao;
	
	@RequestMapping("/useCoupon.do")
	public ModelAndView process(HttpSession session,
								HttpServletRequest request){
		
		String id = (String) session.getAttribute("id");
		String senderid = request.getParameter("senderid");
		String cou_id = request.getParameter("cou_id");
		
		MCouponCommand mCoupon = new MCouponCommand();
		mCoupon.setCou_id(cou_id);
		mCoupon.setSenderid(senderid);
		mCoupon.setReceiverid(id);
		
		int couCount = mCouponDao.mCouCount(mCoupon);
		
		if(couCount > 1){
			mCoupon.setCount(--couCount);
			mCouponDao.updateMCou(mCoupon);
		}else{
			mCouponDao.deleteMCou(mCoupon);
		}
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("useCoupon");
		
		return mav;
	}
	
}
